#PBAR_ExamineBaseRecoveryVer2.py
#
#  Deals with more channels and datasets than the previous version
#
#   8/12/2013, John Kwong

import matplotlib.pyplot as plt
import numpy as np
import PBAR_FD
from scipy import ndimage
from scipy.optimize import curve_fit

plotColors = ['r', 'b', 'g', 'm', 'c', 'y', 'k'] * 10
lineStyles = ['-', ':', '-.', '_', '|'] *  10
markerTypes = ['.', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', 's', 'p', '*', 'h', 'H', '+', 'x', 'D', 'd']

detectorList = {}
detectorList['PS'] = np.arange(25)
detectorList['FC'] = np.arange(25,40)

timeCal = 33.3e-3/256.

# READ IN THE DATA
filePrefixList = ['dz33','dz34','dz35','dz36','dz37','dz38','dz39']
filePrefixList = ['dz33','dz34']


filePath = 'C:\Users\jkwong\Documents\Work\PBAR\data'

dat = []
for i in xrange(len(filePrefixList)):
    dat.append(PBAR_FD.ReadData(filePrefixList[i], filePath))

# Data set groups

# Get count from hydrogen peak and iron peak
# skip the first time bin

def gauss_function(x, a, x0, sigma):
    return a*np.exp(-(x-x0)**2/(2*sigma**2))

def gauss_exp_function(x, a, x0, sigma, b, c ):
    return(a*np.exp(-(x-x0)**2/(2*sigma**2)) + b*exp(x*c))

    
def inverted_exp_function(x, a, b, c):
    return(a * np.exp(x*b) + c)
    

# Create fit parameters

#peakBounds = peakBoundsList[j][k][c]  # length = number of peaks to fit to
#peakLocation = peakLocationList[j][k][c]  # length = number of peaks to fit to    
#timeBoundariesList = timeBoundariesListList[j][k][c]  # time boundaries

# Time boundaries
temp = []
temp.append(np.array((2, 3, 4, 5, 6, 7, 9, 11)))
temp.append(np.array(np.hstack((np.arange(2,20,2), np.arange(20,60,10)))))

timeBoundariesListList = []
for j in xrange(len(filePrefixList)):   # cycle through datasets
    temp2 = []
    for k in xrange(len(dat[j])):  # cycle through dataset channels
        temp2.append(temp)
    timeBoundariesListList.append(temp2)

## Location of the peak
#temp = []
#temp.append([[35,45], [35,44], [35,44], [35,44], [35,44], [35,44], [35,44], [35,44]])
#temp.append([[122,154],[122,154],[122,154],[122,154],[122,154],[122,154],[122,154],[122,154],[122,154],[122,154],[122,154],[122,154],])
#
#peakBoundsListList = []
#for j in xrange(len(filePrefixList)):   # cycle through datasets
#    temp2 = []
#    for k in xrange(len(dat[j])):  # cycle through dataset channels
#        temp2.append(temp)
#    peakBoundsListList.append(temp2)
#    


# PEAK LOCATION
peakLocationListList = []

# dz33, Al peak
temp0 = []

temp3 = []
temp3.append([ 39.8172043 ,  41.1155914 ,  42.41397849,  42.63037634,42.41397849,  43.06317204,  43.06317204])
temp3.append([ 39.56317204,  40.86155914,  41.72715054,  41.94354839, 41.94354839,  41.72715054,  41.29435484])
temp3.append([ 40.60752688,  42.12231183,  43.42069892,  42.98790323, 43.20430108,  42.55510753,  41.90591398])
temp3.append([ 37.32392473,  38.18951613,  38.18951613,  38.18951613, 37.97311828,  38.62231183,  37.54032258])
temp3.append([ 38.26570651,  38.69316698,  39.33435769,  38.90689722,  38.90689722,  39.12062745,  38.26570651])
temp3.append([ 38.0483871,  38.4811828,  38.4811828,  37.83198925,  38.26478495,  38.26478495,  38.4811828 ])
temp3.append([ 45.36827957,  45.58467742,  45.15188172,  45.58467742,  45.58467742,  45.15188172,  43.42069892])
temp3.append([ 44.68145161,  44.89784946,  44.46505376,  44.24865591,  43.81586022,  44.89784946,  42.73387097])
temp3.append([ 36.29223627,  38.01018043,  38.65440949,  38.65440949,  38.65440949,  39.51338157,  38.43966647])
temp3.append([ 34.15322581,  36.10080645,  36.3172043,  37.1827957,  37.6155914,  37.1827957,  37.39919355])
temp3.append([ 32.3844086,  32.8172043,  33.46639785,  33.6827957,  34.1155914,  34.9811828,  33.25])
temp3.append([ 36.02553763,  36.89112903,  37.10752688,  36.89112903,  37.54032258,  37.97311828,  36.89112903])
temp3.append([ 38.65440949,  39.08389553,  38.86915251,  38.01018043,  38.86915251,  38.65440949,  38.86915251])
temp3.append([ 38.26478495,  38.0483871,   37.83198925,  37.6155914,   37.6155914,  37.1827957,   37.39919355])
temp3.append([ 40.60752688,  41.25672043,  41.47311828,  40.39112903,  40.82392473,  40.39112903,  41.04032258])
temp3.append([ 41.65188172,  42.30107527,  41.43548387,  42.30107527,  41.65188172,  41.21908602,  41.86827957])
temp3.append([ 36.88668681,  37.36333385,  38.07830441,  37.83998089,  38.31662793,  37.83998089,  37.60165737])
temp3.append([ 36.10080645,  36.75,        36.96639785,  37.39919355,  37.39919355,  37.1827957,   37.83198925])
temp3.append([ 34.5483871,   35.63037634,  36.27956989,  36.49596774,  36.71236559,  37.14516129,  36.71236559])
temp3.append([ 38.83870968,  40.56989247,  40.78629032,  41.43548387,  41.43548387,  41.65188172,  40.78629032])
temp3.append([ 42.36812778,  42.84477482,  42.84477482,  42.36812778,  42.6064513,  42.12980426,  42.36812778])
temp3.append([ 39.99596774,  39.13037634,  39.99596774,  39.77956989,  39.99596774,  39.56317204,  39.34677419])
temp3.append([ 38.44354839,  38.44354839,  38.87634409,  38.01075269,  38.01075269,  38.22715054,  38.01075269])
temp3.append([ 37.54032258,  37.54032258,  37.32392473,  38.83870968,  37.32392473,  36.89112903,  37.54032258])
temp3.append([ 36.89030704,  37.96895143,  38.18468031,  38.61613807,  39.2633247,  38.40040919,  38.61613807])
temp3.append([ 35.4516129,   36.53360215,  35.8844086,   35.4516129,   36.75,        35.4516129,  35.8844086 ])
temp3.append([ 43.59946237,  43.59946237,  44.68145161,  45.54704301,  43.38306452,  44.03225806,  42.51747312])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3.append([ 36.67457817,  36.24312041,  36.24312041,  36.67457817,  36.02739153,  35.59593378,  35.59593378])
temp3 = np.array(temp3)
temp0.append(temp3)


#dx33, Fe peak location
temp3 = []
temp3.append([ 138.90524194,  145.33696995,  147.78715206,  149.93106139,148.09342482,  147.17460653,  146.86833377,  145.64324272,145.33696995,  145.03069719,  140.43660575,  137.98642365])
temp3.append([ 144.77150538,  150.33602151,  149.71774194,  151.57258065,150.02688172,  149.40860215,  147.24462366,  146.62634409,145.08064516,  145.08064516,  140.44354839,  137.97043011])
temp3.append([ 144.71774194,  146.26344086,  148.42741935,  149.97311828,149.04569892,  150.90053763,  149.35483871,  149.66397849,149.35483871,  146.26344086,  143.17204301,  138.22580645])
temp3.append([ 133.22580645,  132.91666667,  136.62634409,  136.00806452,137.55376344,  135.69892473,  136.3172043 ,  135.69892473,134.77150538,  134.15322581,  131.98924731,  132.60752688])
temp3.append([ 135.84251431,  136.7613326 ,  137.68015088,  137.68015088,137.68015088,  137.68015088,  137.06760536,  137.06760536,134.92369602,  134.92369602,  133.69860497,  134.00487773])
temp3.append([ 133.9516129 ,  133.9516129 ,  134.56989247,  133.02419355,133.64247312,  132.09677419,  133.02419355,  132.09677419,132.40591398,  132.71505376,  131.16935484,  131.16935484])
temp3.append([ 160.37813981,  160.68570066,  160.07057897,  156.99497052,155.14960545,  152.68911868,  152.38155784,  148.99838854,146.53790178,  142.84717164,  140.07912403,  136.38839389])
temp3.append([ 157.58718493,  158.79897965,  160.61667173,  160.01077437,160.01077437,  158.19308229,  156.98128758,  157.58718493,158.49603097,  156.37539022,  154.86064682,  151.22526267])
temp3.append([ 130.11153745,  137.48509084,  138.09955362,  138.40678501,140.25017336,  140.25017336,  138.7140164 ,  138.09955362,137.79232223,  134.41277693,  128.88261188,  126.11752936])
temp3.append([ 121.27688172,  130.86021505,  133.02419355,  134.87903226,137.35215054,  137.97043011,  137.04301075,  137.04301075,136.42473118,  136.1155914 ,  132.40591398,  131.16935484])
temp3.append([ 114.42204301,  119.98655914,  124.31451613,  125.24193548,127.40591398,  128.02419355,  127.71505376,  133.58870968,129.26075269,  126.16935484,  125.24193548,  124.31451613])
temp3.append([ 128.27956989,  131.98924731,  134.46236559,  135.08064516,137.55376344,  137.55376344,  135.08064516,  138.4811828 ,136.3172043 ,  136.62634409,  136.93548387,  134.77150538])
temp3.append([ 139.02124779,  138.7140164 ,  138.7140164 ,  140.25017336,141.47909892,  138.09955362,  138.09955362,  138.09955362,136.25616527,  135.02723971,  129.80430606,  125.81029797])
temp3.append([ 132.71505376,  131.16935484,  130.86021505,  132.09677419,133.33333333,  134.56989247,  133.64247312,  133.64247312,130.24193548,  129.00537634,  127.7688172 ,  127.15053763])
temp3.append([ 147.80913978,  148.42741935,  148.73655914,  149.04569892,149.97311828,  149.97311828,  146.88172043,  146.57258065,146.26344086,  144.09946237,  141.93548387,  138.53494624])
temp3.append([ 143.46614493,  145.00228711,  146.5384293 ,  147.46011461,145.61674399,  145.00228711,  144.69505868,  144.38783024,144.38783024,  143.15891649,  140.701089  ,  136.39989088])
temp3.append([ 130.23966774,  133.06026268,  135.59879812,  134.47056015,135.31673863,  135.03467913,  134.18850065,  133.06026268,131.9320247 ,  131.36790572,  130.52172723,  126.29083483])
temp3.append([ 129.9327957 ,  132.71505376,  134.56989247,  134.56989247,134.87903226,  136.73387097,  134.87903226,  133.64247312,133.9516129 ,  132.09677419,  131.47849462,  128.69623656])
temp3.append([ 133.22580645,  138.17204301,  140.64516129,  141.88172043,141.88172043,  143.11827957,  140.33602151,  139.40860215,140.95430108,  141.26344086,  135.69892473,  136.62634409])
temp3.append([ 143.77852344,  146.03499938,  146.31705888,  144.90676141,143.21440445,  143.21440445,  144.06058293,  143.49646394,143.21440445,  143.49646394,  142.36822597,  139.82969052])
temp3.append([ 139.51612903,  139.51612903,  137.66129032,  139.20698925,138.27956989,  137.04301075,  137.97043011,  137.35215054,137.04301075,  135.80645161,  134.56989247,  131.16935484])
temp3.append([ 135.44354839,  135.1344086 ,  135.1344086 ,  135.75268817,133.89784946,  132.35215054,  132.97043011,  132.97043011,132.35215054,  130.80645161,  130.18817204,  130.49731183])
temp3.append([ 129.82526882,  130.75268817,  132.60752688,  131.98924731,132.2983871 ,  131.68010753,  131.06182796,  130.44354839,131.06182796,  130.44354839,  130.44354839,  129.82526882])
temp3.append([ 125.26755864,  129.64152644,  130.37052107,  131.46401302,134.37999156,  133.65099692,  134.74448887,  135.83798082,134.01549424,  134.01549424,  132.19300766,  128.54803449])
temp3.append([ 132.05741627,  131.57894737,  133.49282297,  133.49282297,132.05741627,  129.66507177,  130.62200957,  131.57894737,131.10047847,  129.66507177,  128.70813397,  123.92344498])
temp3.append([ 139.2,  140. ,  138. ,  138.8,  138.4,  138. ,  136.8,  136.8,138. ,  136.4,  137.2,  132.4])
temp3.append([ 160.07259471,  162.71708607,  161.96151711,  160.82816367,161.20594815,  158.5614568 ,  157.80588784,  155.16139648,155.53918096,  157.05031888,  149.4946293 ,  144.96121555])
temp3.append([ 133.65099692,  134.01549424,  133.28649961,  134.74448887,131.09951571,  131.82851034,  132.19300766,  133.28649961,132.19300766,  129.27702912,  129.27702912,  128.18353717])
temp3.append([ 125.83732057,  125.83732057,  124.40191388,  124.88038278,123.44497608,  122.96650718,  122.48803828,  121.05263158,119.61722488,  119.13875598,  118.66028708,  120.09569378])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3 = np.array(temp3)
temp0.append(temp3)

# reformat the array
peakLocationListTemp = []
for i in xrange(40):
    temp = []
    temp.append(temp0[0][i])
    temp.append(temp0[1][i])    
    peakLocationListTemp.append(temp)

peakLocationListList.append(peakLocationListTemp)

#for j in xrange(len(filePrefixList)):   # cycle through datasets
    #peakLocationListList.append(peakLocationListTemp)


# dz34, H peak
temp0 = []

temp3 = []
temp3.append([ 39.62734332,  41.0015059 ,  42.14664139,  42.37566849,42.60469559,  42.14664139,  42.14664139])
temp3.append([ 39.375     ,  41.22983871,  41.46169355,  41.46169355,42.15725806,  41.92540323,  41.22983871])
temp3.append([ 41.42137097,  42.34879032,  43.27620968,  42.58064516,43.04435484,  42.8125    ,  42.58064516])
temp3.append([ 38.13508065,  38.59879032,  38.59879032,  38.13508065,38.83064516,  39.52620968,  39.0625    ])
temp3.append([ 38.71123493,  38.94026203,  38.71123493,  39.16928913,39.39831622,  38.71123493,  39.39831622])
temp3.append([ 38.21572581,  38.91129032,  39.14314516,  39.14314516,39.14314516,  39.14314516,  38.67943548])
temp3.append([ 44.89919355,  45.13104839,  44.89919355,  44.89919355,44.20362903,  43.04435484,  42.8125    ])
temp3.append([ 44.85887097,  45.09072581,  45.32258065,  45.55443548,45.55443548,  44.62701613,  44.85887097])

temp3.append([ 37.04653317,  38.42701708,  39.11725903,  38.65709773,38.88717838,  39.11725903,  38.65709773])
temp3.append([ 35.2016129 ,  36.59274194,  37.05645161,  38.21572581,38.21572581,  38.91129032,  39.375     ])
temp3.append([ 30.98790323,  32.6108871 ,  33.07459677,  33.53830645,34.23387097,  34.23387097,  35.39314516])
temp3.append([ 30.25201613,  31.875     ,  33.49798387,  34.42540323,34.8891129 ,  36.0483871 ,  36.74395161])
temp3.append([ 27.84330714,  29.45387169,  30.3741943 ,  30.3741943 ,31.52459755,  30.8343556 ,  31.06443625])
temp3.append([ 19.66733871,  20.36290323,  21.29032258,  21.9858871 ,21.9858871 ,  22.91330645,  24.30443548])
temp3.append([ 26.35080645,  29.13306452,  31.4516129 ,  33.53830645,35.85685484,  37.94354839,  38.87096774])
temp3.append([ 32.33870968,  33.26612903,  34.65725806,  35.12096774,36.0483871 ,  37.67137097,  38.83064516])

temp3.append([ 31.88717047,  35.09832704,  36.93327366,  37.39201031,38.76822027,  38.9975886 ,  39.22695693])
temp3.append([ 35.43346774,  36.3608871 ,  36.59274194,  37.75201613,37.05645161,  37.98387097,  37.52016129])
temp3.append([ 35.85685484,  36.32056452,  36.55241935,  37.47983871,37.71169355,  38.17540323,  37.71169355])
temp3.append([ 39.52620968,  40.91733871,  41.61290323,  41.84475806,42.54032258,  42.0766129 ,  42.54032258])
temp3.append([ 41.97937685,  43.12621848,  43.58495514,  43.58495514,44.04369179,  43.12621848,  42.66748183])
temp3.append([ 40.53427419,  40.53427419,  39.83870968,  40.53427419,40.30241935,  40.53427419,  40.30241935])
temp3.append([ 39.10282258,  38.87096774,  38.87096774,  38.6391129 ,39.7983871 ,  38.6391129 ,  38.17540323])
temp3.append([ 19.81854839,  21.20967742,  21.44153226,  22.36895161,22.60080645,  22.36895161,  23.6875    ])

temp3.append([ 37.10310325,  38.48582759,  38.48582759,  38.94673571,39.17718976,  39.40764382,  39.17718976])
temp3.append([ 37.28830645,  37.75201613,  38.21572581,  37.52016129,37.75201613,  37.75201613,  36.3608871 ])
temp3.append([ 37.47983871,  37.71169355,  38.40725806,  37.01612903,37.24798387,  37.47983871,  37.47983871])
temp3.append([ 43.46774194,  43.69959677,  43.69959677,  42.77217742,43.2358871 ,  43.46774194,  43.2358871 ])
temp3.append([ 36.18128702,  36.64219513,  35.95083296,  36.41174107,36.41174107,  36.87264919,  36.87264919])
temp3.append([ 35.43346774,  35.89717742,  35.66532258,  35.66532258,36.12903226,  35.66532258,  35.66532258])
temp3.append([ 30.29233871,  29.59677419,  30.06048387,  29.36491935,29.59677419,  28.90120968,  29.36491935])
temp3.append([ 33.03427419,  32.80241935,  33.03427419,  32.80241935,32.80241935,  33.03427419,  33.49798387])

temp3.append([ 30.60044469,  31.06060175,  31.06060175,  30.60044469,31.29068028,  31.29068028,  30.60044469])
temp3.append([ 36.2927208 ,  36.52267468,  36.06276692,  35.60285916,36.06276692,  36.2927208 ,  35.83281304])
temp3.append([ 34.69758065,  34.00201613,  33.53830645,  33.53830645,34.23387097,  34.23387097,  33.30645161])
temp3.append([ 39.75806452,  39.75806452,  39.29435484,  39.29435484,38.83064516,  39.29435484,  39.29435484])
temp3.append([ 39.75806452,  39.75806452,  39.29435484,  39.29435484,38.83064516,  39.29435484,  39.29435484])
temp3.append([ 40.20193673,  39.05216734,  39.05216734,  39.05216734,38.3623057 ,  39.05216734,  39.28212122])
temp3.append([ 33.53830645,  35.39314516,  34.23387097,  34.23387097,34.23387097,  33.53830645,  33.53830645])
temp3.append([ 34.19354839,  34.65725806,  34.42540323,  33.96169355,34.42540323,  34.8891129 ,  34.8891129 ])
temp3 = np.array(temp3)
temp0.append(temp3)

#dz34, Fe peak location - just copied from dz33

temp3 = []
temp3.append([ 138.90524194,  145.33696995,  147.78715206,  149.93106139,148.09342482,  147.17460653,  146.86833377,  145.64324272,145.33696995,  145.03069719,  140.43660575,  137.98642365])
temp3.append([ 144.77150538,  150.33602151,  149.71774194,  151.57258065,150.02688172,  149.40860215,  147.24462366,  146.62634409,145.08064516,  145.08064516,  140.44354839,  137.97043011])
temp3.append([ 144.71774194,  146.26344086,  148.42741935,  149.97311828,149.04569892,  150.90053763,  149.35483871,  149.66397849,149.35483871,  146.26344086,  143.17204301,  138.22580645])
temp3.append([ 133.22580645,  132.91666667,  136.62634409,  136.00806452,137.55376344,  135.69892473,  136.3172043 ,  135.69892473,134.77150538,  134.15322581,  131.98924731,  132.60752688])
temp3.append([ 135.84251431,  136.7613326 ,  137.68015088,  137.68015088,137.68015088,  137.68015088,  137.06760536,  137.06760536,134.92369602,  134.92369602,  133.69860497,  134.00487773])
temp3.append([ 133.9516129 ,  133.9516129 ,  134.56989247,  133.02419355,133.64247312,  132.09677419,  133.02419355,  132.09677419,132.40591398,  132.71505376,  131.16935484,  131.16935484])
temp3.append([ 160.37813981,  160.68570066,  160.07057897,  156.99497052,155.14960545,  152.68911868,  152.38155784,  148.99838854,146.53790178,  142.84717164,  140.07912403,  136.38839389])
temp3.append([ 157.58718493,  158.79897965,  160.61667173,  160.01077437,160.01077437,  158.19308229,  156.98128758,  157.58718493,158.49603097,  156.37539022,  154.86064682,  151.22526267])
temp3.append([ 130.11153745,  137.48509084,  138.09955362,  138.40678501,140.25017336,  140.25017336,  138.7140164 ,  138.09955362,137.79232223,  134.41277693,  128.88261188,  126.11752936])
temp3.append([ 121.27688172,  130.86021505,  133.02419355,  134.87903226,137.35215054,  137.97043011,  137.04301075,  137.04301075,136.42473118,  136.1155914 ,  132.40591398,  131.16935484])
temp3.append([ 114.42204301,  119.98655914,  124.31451613,  125.24193548,127.40591398,  128.02419355,  127.71505376,  133.58870968,129.26075269,  126.16935484,  125.24193548,  124.31451613])
temp3.append([ 128.27956989,  131.98924731,  134.46236559,  135.08064516,137.55376344,  137.55376344,  135.08064516,  138.4811828 ,136.3172043 ,  136.62634409,  136.93548387,  134.77150538])
temp3.append([ 139.02124779,  138.7140164 ,  138.7140164 ,  140.25017336,141.47909892,  138.09955362,  138.09955362,  138.09955362,136.25616527,  135.02723971,  129.80430606,  125.81029797])
temp3.append([ 132.71505376,  131.16935484,  130.86021505,  132.09677419,133.33333333,  134.56989247,  133.64247312,  133.64247312,130.24193548,  129.00537634,  127.7688172 ,  127.15053763])
temp3.append([ 147.80913978,  148.42741935,  148.73655914,  149.04569892,149.97311828,  149.97311828,  146.88172043,  146.57258065,146.26344086,  144.09946237,  141.93548387,  138.53494624])
temp3.append([ 143.46614493,  145.00228711,  146.5384293 ,  147.46011461,145.61674399,  145.00228711,  144.69505868,  144.38783024,144.38783024,  143.15891649,  140.701089  ,  136.39989088])
temp3.append([ 130.23966774,  133.06026268,  135.59879812,  134.47056015,135.31673863,  135.03467913,  134.18850065,  133.06026268,131.9320247 ,  131.36790572,  130.52172723,  126.29083483])
temp3.append([ 129.9327957 ,  132.71505376,  134.56989247,  134.56989247,134.87903226,  136.73387097,  134.87903226,  133.64247312,133.9516129 ,  132.09677419,  131.47849462,  128.69623656])
temp3.append([ 133.22580645,  138.17204301,  140.64516129,  141.88172043,141.88172043,  143.11827957,  140.33602151,  139.40860215,140.95430108,  141.26344086,  135.69892473,  136.62634409])
temp3.append([ 143.77852344,  146.03499938,  146.31705888,  144.90676141,143.21440445,  143.21440445,  144.06058293,  143.49646394,143.21440445,  143.49646394,  142.36822597,  139.82969052])
temp3.append([ 139.51612903,  139.51612903,  137.66129032,  139.20698925,138.27956989,  137.04301075,  137.97043011,  137.35215054,137.04301075,  135.80645161,  134.56989247,  131.16935484])
temp3.append([ 135.44354839,  135.1344086 ,  135.1344086 ,  135.75268817,133.89784946,  132.35215054,  132.97043011,  132.97043011,132.35215054,  130.80645161,  130.18817204,  130.49731183])
temp3.append([ 129.82526882,  130.75268817,  132.60752688,  131.98924731,132.2983871 ,  131.68010753,  131.06182796,  130.44354839,131.06182796,  130.44354839,  130.44354839,  129.82526882])
temp3.append([ 125.26755864,  129.64152644,  130.37052107,  131.46401302,134.37999156,  133.65099692,  134.74448887,  135.83798082,134.01549424,  134.01549424,  132.19300766,  128.54803449])
temp3.append([ 132.05741627,  131.57894737,  133.49282297,  133.49282297,132.05741627,  129.66507177,  130.62200957,  131.57894737,131.10047847,  129.66507177,  128.70813397,  123.92344498])
temp3.append([ 139.2,  140. ,  138. ,  138.8,  138.4,  138. ,  136.8,  136.8,138. ,  136.4,  137.2,  132.4])
temp3.append([ 160.07259471,  162.71708607,  161.96151711,  160.82816367,161.20594815,  158.5614568 ,  157.80588784,  155.16139648,155.53918096,  157.05031888,  149.4946293 ,  144.96121555])
temp3.append([ 133.65099692,  134.01549424,  133.28649961,  134.74448887,131.09951571,  131.82851034,  132.19300766,  133.28649961,132.19300766,  129.27702912,  129.27702912,  128.18353717])
temp3.append([ 125.83732057,  125.83732057,  124.40191388,  124.88038278,123.44497608,  122.96650718,  122.48803828,  121.05263158,119.61722488,  119.13875598,  118.66028708,  120.09569378])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3.append([ 109.44947451,  113.2273193 ,  112.47175034,  114.36067274,113.60510378,  113.60510378,  114.36067274,  113.98288826,113.60510378,  112.84953482,  112.84953482,  112.84953482,112.84953482,  112.84953482])
temp3 = np.array(temp3)
temp0.append(temp3)

# reformat the array
peakLocationListTemp = []
for i in xrange(40):
    temp = []
    temp.append(temp0[0][i])
    temp.append(temp0[1][i])    
    peakLocationListTemp.append(temp)

peakLocationListList.append(peakLocationListTemp)


# LOWER BOUND
peakLowerBoundsListList = []

temp0 = []

# dx33, Al, lower bound
temp3 = []
for i in xrange(40):
    temp3.append([20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0])
#temp3.append([ 33.61895161,  34.31451613,  35.24193548,  35.47379032,36.16935484,  36.63306452,  37.09677419])
#temp3.append([ 30.79637097,  32.65120968,  32.65120968,  33.57862903,33.81048387,  34.96975806,  36.3608871 ])
#temp3.append([ 32.84274194,  33.30645161,  35.39314516,  34.23387097,35.16129032,  35.85685484,  36.78427419])
#temp3.append([ 29.78830645,  30.02016129,  31.64314516,  31.875     ,31.875     ,  32.33870968,  31.875     ])
#temp3.append([ 32.62450003,  33.08390918,  33.77302289,  34.46213661,34.92154575,  35.61065947,  35.61065947])
#temp3.append([ 31.72379032,  32.65120968,  32.1875    ,  33.11491935,32.88306452,  33.57862903,  33.57862903])
#temp3.append([ 37.71169355,  38.6391129 ,  38.87096774,  38.6391129 ,38.17540323,  38.6391129 ,  39.7983871 ])
#temp3.append([ 36.97580645,  38.36693548,  36.97580645,  37.67137097, 37.90322581,  37.43951613,  36.97580645])
#temp3.append([ 29.0359543 ,  29.95763961,  31.34016758,  31.5705889 ,32.03143156,  32.26185289,  31.5705889 ])
#temp3.append([ 26.85483871,  28.24596774,  29.1733871 ,  29.63709677,31.02822581,  31.02822581,  31.02822581])
#temp3.append([ 25.88709677,  26.35080645,  26.58266129,  26.81451613,27.51008065,  27.74193548,  30.29233871])
#temp3.append([ 29.32459677,  29.78830645,  30.48387097,  30.25201613,30.48387097,  30.71572581,  30.71572581])
#temp3.append([ 31.34016758,  32.72269554,  32.03143156,  31.5705889 ,32.49227422,  32.49227422,  31.80101023])
#temp3.append([ 30.33266129,  30.79637097,  30.56451613,  30.56451613,31.72379032,  31.26008065,  31.26008065])
#temp3.append([ 32.84274194,  33.77016129,  33.53830645,  34.00201613,34.23387097,  34.46572581,  35.16129032])
#temp3.append([ 35.58467742,  34.65725806,  34.42540323,  34.8891129 ,35.12096774,  36.74395161,  36.0483871 ])
#temp3.append([ 30.78601048,  31.24542849,  32.62368251,  32.1642645 ,33.08310051,  33.08310051,  33.31280952])
#temp3.append([ 28.70967742,  29.86895161,  30.10080645,  31.02822581,30.56451613,  31.02822581,  29.40524194])
#temp3.append([ 29.13306452,  30.06048387,  30.52419355,  30.98790323,30.52419355,  31.21975806,  30.29233871])
#temp3.append([ 31.17943548,  33.49798387,  33.26612903,  33.03427419,35.58467742,  36.28024194,  35.58467742])
#temp3.append([ 35.15048155,  36.98815358,  37.67728059,  37.21786258,35.60989955,  36.98815358,  36.06931756])
#temp3.append([ 32.41935484,  33.57862903,  33.57862903,  35.2016129 ,34.50604839,  35.2016129 ,  34.96975806])
#temp3.append([ 31.4516129 ,  31.68346774,  31.21975806,  31.4516129 ,30.75604839,  31.4516129 ,  31.4516129 ])
#temp3.append([ 30.02016129,  30.48387097,  30.94758065,  30.48387097,29.78830645,  30.48387097,  29.78830645])
#temp3.append([ 30.55630147,  31.47513749,  31.70484649,  33.08310051,32.62368251,  34.46135454,  34.46135454])
#temp3.append([ 29.1733871 ,  30.10080645,  30.33266129,  30.33266129,30.33266129,  30.79637097,  29.40524194])
#temp3.append([ 27.27822581,  27.97379032,  28.90120968,  28.66935484,30.06048387,  29.82862903,  27.97379032])
#temp3.append([ 33.96169355,  33.26612903,  34.8891129 ,  34.8891129 ,33.96169355,  32.33870968,  32.33870968])
#temp3.append([ 26.88095742,  27.11066642,  27.11066642,  27.79979343,28.48892044,  28.02950244,  28.02950244])
#temp3.append([ 25.        ,  25.69556452,  25.92741935,  22.91330645,23.84072581,  24.76814516,  23.37701613])
#temp3.append([ 21.94556452,  20.09072581,  19.39516129,  20.78629032,19.62701613,  19.62701613,  19.39516129])
#temp3.append([ 20.74596774,  20.5141129 ,  20.5141129 ,  20.5141129 ,19.12298387,  19.35483871,  19.12298387])
#temp3.append([ 19.47543249,  20.62037157,  19.93340813,  19.93340813,20.39138376,  19.93340813,  19.24644468])
#temp3.append([ 20.96495892,  22.58379807,  24.43389996,  23.04632355,22.81506081,  23.04632355,  21.42748439])
#temp3.append([ 24.03225806,  23.56854839,  23.56854839,  23.56854839,23.56854839,  22.87298387,  22.64112903])
#temp3.append([ 24.6875    ,  25.15120968,  24.6875    ,  24.22379032,24.45564516,  23.76008065,  23.29637097])
#temp3.append([ 20.84935939,  20.84935939,  20.84935939,  20.84935939,20.84935939,  20.84935939,  20.84935939])
#temp3.append([ 25.35895091,  24.89642544,  24.89642544,  24.6651627 ,24.20263723,  24.20263723,  23.50884902])
#temp3.append([ 23.33669355,  23.56854839,  23.80040323,  24.03225806,23.80040323,  23.80040323,  22.87298387])
#temp3.append([ 21.6733871 ,  21.20967742,  21.20967742,  21.20967742,20.97782258,  20.97782258,  21.20967742])
temp3 = np.array(temp3)
temp0.append(temp3)

# dz33, Fe peak, lower bound
temp3 = []
temp3.append([ 128.491968  ,  129.71705905,  130.9421501 ,  130.9421501 ,130.32960458,  132.16724116,  132.47351392,  133.39233221, 133.39233221,  129.41078629,  129.10451353,  127.26687695])
temp3.append([ 130.49731183,  131.73387097,  132.35215054,  132.66129032,133.27956989,  135.1344086 ,  133.89784946,  134.82526882,135.1344086 ,  134.20698925,  134.20698925,  128.9516129 ])
temp3.append([ 132.35215054,  134.82526882,  135.44354839,  137.2983871 ,137.2983871 ,  137.60752688,  136.68010753,  136.37096774,135.75268817,  135.75268817,  133.27956989,  127.71505376])
temp3.append([ 121.16935484,  121.47849462,  122.09677419,  122.09677419,122.71505376,  123.33333333,  121.78763441,  121.78763441,122.71505376,  121.78763441,  120.24193548,  120.55107527])
temp3.append([ 121.16935484,  121.47849462,  122.09677419,  122.09677419,122.71505376,  123.33333333,  121.78763441,  121.78763441,122.71505376,  121.78763441,  120.24193548,  120.55107527])
temp3.append([ 119.42204301,  119.7311828 ,  120.65860215,  121.58602151,120.96774194,  119.7311828 ,  120.34946237,  119.11290323,120.04032258,  120.65860215,  119.42204301,  117.25806452])
temp3.append([ 144.09946237,  146.26344086,  143.4811828 ,  143.79032258,141.62634409,  139.77150538,  137.2983871 ,  136.06182796,137.2983871 ,  133.89784946,  128.64247312,  122.7688172 ])
temp3.append([ 140.95430108,  141.88172043,  141.88172043,  141.26344086,141.26344086,  141.57258065,  141.57258065,  141.57258065,141.57258065,  141.57258065,  140.33602151,  140.02688172,135.38978495])
temp3.append([ 119.41655466,  121.57384344,  120.95747521,  123.11476399,124.65568454,  125.88842099,  122.49839577,  122.80657988,120.34110699,  119.72473877,  119.10837055,  116.64289766])
temp3.append([ 110.76612903,  118.49462366,  120.96774194,  121.58602151,123.75      ,  122.82258065,  120.96774194,  123.13172043,120.65860215,  123.44086022,  122.20430108,  116.02150538])
temp3.append([ 105.76612903,  108.5483871 ,  113.80376344,  113.18548387,114.42204301,  115.96774194,  114.42204301,  114.7311828 ,115.65860215,  117.20430108,  114.42204301,  114.7311828 ])
temp3.append([ 116.84139785,  118.38709677,  119.00537634,  123.33333333,124.87903226,  124.87903226,  123.33333333,  123.33333333,124.26075269,  124.26075269,  123.64247312,  122.40591398])
temp3.append([ 125.50306658,  126.73199215,  127.03922354,  127.34645493,124.8886038 ,  125.19583519,  123.04521545,  121.5090585 ,123.35244684,  122.12352128,  120.28013293,  116.28612485])
temp3.append([ 120.04032258,  120.65860215,  119.11290323,  117.87634409,120.96774194,  120.96774194,  120.96774194,  120.34946237,121.27688172,  120.34946237,  117.5672043 ,  115.71236559])
temp3.append([ 134.51612903,  134.51612903,  133.89784946,  134.20698925,132.97043011,  132.04301075,  129.87903226,  130.49731183,130.80645161,  126.16935484,  123.69623656,  124.62365591])
temp3.append([ 129.82526882,  132.2983871 ,  133.84408602,  131.98924731,134.15322581,  131.98924731,  128.58870968,  127.97043011,127.97043011,  127.97043011,  126.73387097,  125.49731183])
temp3.append([ 119.80346648,  122.90612091,  124.03435888,  121.77788293,124.31641837,  122.05994243,  118.957288  ,  119.52140698,122.62406141,  120.93170445,  116.98287154,  116.13669306])
temp3.append([ 118.49462366,  119.42204301,  119.11290323,  121.27688172,121.27688172,  119.7311828 ,  120.04032258,  120.34946237,120.34946237,  118.49462366,  118.49462366,  117.25806452])
temp3.append([ 111.33064516,  111.94892473,  115.65860215,  117.51344086,119.05913978,  119.05913978,  118.44086022,  119.05913978,119.05913978,  121.22311828,  121.22311828,  119.36827957])
temp3.append([ 123.33333333,  123.64247312,  126.42473118,  128.58870968,130.75268817,  128.89784946,  128.89784946,  128.89784946,121.47849462,  126.42473118,  122.40591398,  124.87903226])
temp3.append([ 131.36790572,  132.2140842 ,  133.90644116,  131.64996521,130.80378673,  130.80378673,  133.62438166,  130.80378673,130.52172723,  127.98319179,  126.00877534,  123.75229939])
temp3.append([ 125.29569892,  124.98655914,  123.13172043,  124.36827957,124.05913978,  125.29569892,  125.29569892,  122.51344086,124.05913978,  122.20430108,  123.13172043,  120.34946237])
temp3.append([ 121.53225806,  120.91397849,  121.53225806,  123.38709677,119.98655914,  120.29569892,  119.67741935,  119.36827957,119.36827957,  119.67741935,  118.75      ,  116.89516129])
temp3.append([ 118.07795699,  119.62365591,  120.86021505,  119.31451613,118.38709677,  117.45967742,  118.69623656,  118.69623656,117.15053763,  119.31451613,  117.45967742,  117.45967742])
temp3.append([ 114.97253722,  118.02582628,  120.46845753,  121.07911534,120.77378644,  122.30043097,  123.52174659,  121.99510206,123.8270755 ,  123.21641769,  121.68977316,  118.02582628])
temp3.append([ 120.65860215,  120.34946237,  119.42204301,  119.7311828 ,118.80376344,  118.18548387,  116.63978495,  116.02150538,116.63978495,  116.63978495,  113.5483871 ,  112.62096774])
temp3.append([ 126.16935484,  125.55107527,  124.9327957 ,  123.69623656,124.00537634,  123.07795699,  120.91397849,  121.22311828,118.13172043,  118.44086022,  119.05913978,  115.34946237])
temp3.append([ 139.18950348,  141.35028255,  140.11555165,  138.57213804,135.79399353,  132.08980084,  137.02872442,  133.32453174,132.70716629,  131.4724354 ,  130.54638723,  126.84219455])
temp3.append([ 119.85779972,  119.85779972,  120.46845753,  120.16312863,120.16312863,  120.46845753,  119.55247081,  119.24714191,119.85779972,  118.63648409,  118.63648409,  114.97253722])
temp3.append([ 110.52631579,  111.48325359,  108.13397129,  109.56937799,109.56937799,  110.52631579,  107.65550239,  109.09090909,108.13397129,  106.69856459,  100.9569378 ,  100.        ])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3 = np.array(temp3)
temp0.append(temp3)

# reformat the array
peakLowerBoundsListTemp = []
for i in xrange(40):
    temp = []
    temp.append(temp0[0][i])
    temp.append(temp0[1][i])
    peakLowerBoundsListTemp.append(temp)
    
peakLowerBoundsListList.append(peakLowerBoundsListTemp)



# dx34, Al, lower bound
temp0 = []

temp3 = []
for i in xrange(8):
    temp3.append([20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0])
for i in xrange(4):
    temp3.append([18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 18.0])
temp3.append([15.0, 15.0, 15.0, 15.0, 15.0, 15.0, 15.0])
temp3.append([12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0])
temp3.append([15.0, 15.0, 15.0, 15.0, 15.0, 15.0, 15.0])
temp3.append([16.0, 16.0, 16.0, 16.0, 16.0, 16.0, 16.0])
#17
for i in xrange(3):
    temp3.append([18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 18.0])
temp3.append([20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0])
temp3.append([25.0, 25.0, 25.0, 25.0, 25.0, 25.0, 25.0])
temp3.append([20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0])
temp3.append([20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0])
temp3.append([10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0])

#25
temp3.append([ 21.89313546,  21.89313546,  21.89313546,  21.43222734,21.20177328,  21.89313546,  21.6626814 ])
temp3.append([ 21.9858871 ,  21.29032258,  21.52217742,  21.75403226, 20.8266129 ,  20.8266129 ,  21.75403226])
temp3.append([ 20.55443548,  20.78629032,  20.78629032,  20.55443548,20.55443548,  21.48185484,  20.55443548])
temp3.append([ 22.60080645,  22.60080645,  22.83266129,  23.29637097,22.83266129,  23.29637097,  22.83266129])
temp3.append([ 20.74086517,  21.43222734,  21.20177328,  21.20177328,20.97131923,  21.20177328,  21.20177328])
temp3.append([ 21.29032258,  21.29032258,  21.9858871 ,  21.9858871 ,21.9858871 ,  22.21774194,  21.52217742])
temp3.append([ 19.85887097,  19.62701613,  19.62701613,  19.62701613,19.62701613,  19.62701613,  19.62701613])
temp3.append([ 21.44153226,  21.20967742,  20.74596774,  21.20967742,20.97782258,  21.20967742,  21.20967742])
#33
temp3.append([ 20.70706783,  20.93714636,  20.4769893 ,  20.70706783,20.93714636,  20.24691077,  21.1672249 ])
temp3.append([ 21.57567258,  21.34571871,  21.80562646,  21.80562646,21.80562646,  21.80562646,  21.11576483])
temp3.append([ 21.71370968,  21.94556452,  21.48185484,  21.48185484,22.17741935,  21.94556452,  22.17741935])
temp3.append([ 21.44153226,  20.97782258,  21.44153226,  20.5141129 ,20.97782258,  21.20967742,  21.6733871 ])
temp3.append([ 21.44153226,  20.97782258,  21.44153226,  20.5141129 ,20.97782258,  21.20967742,  21.6733871 ])
temp3.append([ 21.57567258,  20.88581095,  21.57567258,  21.57567258,21.80562646,  22.26553422,  21.57567258])
temp3.append([ 22.17741935,  21.71370968,  22.17741935,  22.17741935,22.40927419,  22.40927419,  22.40927419])
temp3.append([ 20.74596774,  21.20967742,  20.97782258,  21.20967742,20.74596774,  21.44153226,  21.20967742])

temp3 = np.array(temp3)
temp0.append(temp3)

temp3 = []
temp3.append([ 128.491968  ,  129.71705905,  130.9421501 ,  130.9421501 ,130.32960458,  132.16724116,  132.47351392,  133.39233221, 133.39233221,  129.41078629,  129.10451353,  127.26687695])
temp3.append([ 130.49731183,  131.73387097,  132.35215054,  132.66129032,133.27956989,  135.1344086 ,  133.89784946,  134.82526882,135.1344086 ,  134.20698925,  134.20698925,  128.9516129 ])
temp3.append([ 132.35215054,  134.82526882,  135.44354839,  137.2983871 ,137.2983871 ,  137.60752688,  136.68010753,  136.37096774,135.75268817,  135.75268817,  133.27956989,  127.71505376])
temp3.append([ 121.16935484,  121.47849462,  122.09677419,  122.09677419,122.71505376,  123.33333333,  121.78763441,  121.78763441,122.71505376,  121.78763441,  120.24193548,  120.55107527])
temp3.append([ 121.16935484,  121.47849462,  122.09677419,  122.09677419,122.71505376,  123.33333333,  121.78763441,  121.78763441,122.71505376,  121.78763441,  120.24193548,  120.55107527])
temp3.append([ 119.42204301,  119.7311828 ,  120.65860215,  121.58602151,120.96774194,  119.7311828 ,  120.34946237,  119.11290323,120.04032258,  120.65860215,  119.42204301,  117.25806452])
temp3.append([ 144.09946237,  146.26344086,  143.4811828 ,  143.79032258,141.62634409,  139.77150538,  137.2983871 ,  136.06182796,137.2983871 ,  133.89784946,  128.64247312,  122.7688172 ])
temp3.append([ 140.95430108,  141.88172043,  141.88172043,  141.26344086,141.26344086,  141.57258065,  141.57258065,  141.57258065,141.57258065,  141.57258065,  140.33602151,  140.02688172,135.38978495])
temp3.append([ 119.41655466,  121.57384344,  120.95747521,  123.11476399,124.65568454,  125.88842099,  122.49839577,  122.80657988,120.34110699,  119.72473877,  119.10837055,  116.64289766])
temp3.append([ 110.76612903,  118.49462366,  120.96774194,  121.58602151,123.75      ,  122.82258065,  120.96774194,  123.13172043,120.65860215,  123.44086022,  122.20430108,  116.02150538])
temp3.append([ 105.76612903,  108.5483871 ,  113.80376344,  113.18548387,114.42204301,  115.96774194,  114.42204301,  114.7311828 ,115.65860215,  117.20430108,  114.42204301,  114.7311828 ])
temp3.append([ 116.84139785,  118.38709677,  119.00537634,  123.33333333,124.87903226,  124.87903226,  123.33333333,  123.33333333,124.26075269,  124.26075269,  123.64247312,  122.40591398])
temp3.append([ 125.50306658,  126.73199215,  127.03922354,  127.34645493,124.8886038 ,  125.19583519,  123.04521545,  121.5090585 ,123.35244684,  122.12352128,  120.28013293,  116.28612485])
temp3.append([ 120.04032258,  120.65860215,  119.11290323,  117.87634409,120.96774194,  120.96774194,  120.96774194,  120.34946237,121.27688172,  120.34946237,  117.5672043 ,  115.71236559])
temp3.append([ 134.51612903,  134.51612903,  133.89784946,  134.20698925,132.97043011,  132.04301075,  129.87903226,  130.49731183,130.80645161,  126.16935484,  123.69623656,  124.62365591])
temp3.append([ 129.82526882,  132.2983871 ,  133.84408602,  131.98924731,134.15322581,  131.98924731,  128.58870968,  127.97043011,127.97043011,  127.97043011,  126.73387097,  125.49731183])
temp3.append([ 119.80346648,  122.90612091,  124.03435888,  121.77788293,124.31641837,  122.05994243,  118.957288  ,  119.52140698,122.62406141,  120.93170445,  116.98287154,  116.13669306])
temp3.append([ 118.49462366,  119.42204301,  119.11290323,  121.27688172,121.27688172,  119.7311828 ,  120.04032258,  120.34946237,120.34946237,  118.49462366,  118.49462366,  117.25806452])
temp3.append([ 111.33064516,  111.94892473,  115.65860215,  117.51344086,119.05913978,  119.05913978,  118.44086022,  119.05913978,119.05913978,  121.22311828,  121.22311828,  119.36827957])
temp3.append([ 123.33333333,  123.64247312,  126.42473118,  128.58870968,130.75268817,  128.89784946,  128.89784946,  128.89784946,121.47849462,  126.42473118,  122.40591398,  124.87903226])
temp3.append([ 131.36790572,  132.2140842 ,  133.90644116,  131.64996521,130.80378673,  130.80378673,  133.62438166,  130.80378673,130.52172723,  127.98319179,  126.00877534,  123.75229939])
temp3.append([ 125.29569892,  124.98655914,  123.13172043,  124.36827957,124.05913978,  125.29569892,  125.29569892,  122.51344086,124.05913978,  122.20430108,  123.13172043,  120.34946237])
temp3.append([ 121.53225806,  120.91397849,  121.53225806,  123.38709677,119.98655914,  120.29569892,  119.67741935,  119.36827957,119.36827957,  119.67741935,  118.75      ,  116.89516129])
temp3.append([ 118.07795699,  119.62365591,  120.86021505,  119.31451613,118.38709677,  117.45967742,  118.69623656,  118.69623656,117.15053763,  119.31451613,  117.45967742,  117.45967742])
temp3.append([ 114.97253722,  118.02582628,  120.46845753,  121.07911534,120.77378644,  122.30043097,  123.52174659,  121.99510206,123.8270755 ,  123.21641769,  121.68977316,  118.02582628])
temp3.append([ 120.65860215,  120.34946237,  119.42204301,  119.7311828 ,118.80376344,  118.18548387,  116.63978495,  116.02150538,116.63978495,  116.63978495,  113.5483871 ,  112.62096774])
temp3.append([ 126.16935484,  125.55107527,  124.9327957 ,  123.69623656,124.00537634,  123.07795699,  120.91397849,  121.22311828,118.13172043,  118.44086022,  119.05913978,  115.34946237])
temp3.append([ 139.18950348,  141.35028255,  140.11555165,  138.57213804,135.79399353,  132.08980084,  137.02872442,  133.32453174,132.70716629,  131.4724354 ,  130.54638723,  126.84219455])
temp3.append([ 119.85779972,  119.85779972,  120.46845753,  120.16312863,120.16312863,  120.46845753,  119.55247081,  119.24714191,119.85779972,  118.63648409,  118.63648409,  114.97253722])
temp3.append([ 110.52631579,  111.48325359,  108.13397129,  109.56937799,109.56937799,  110.52631579,  107.65550239,  109.09090909,108.13397129,  106.69856459,  100.9569378 ,  100.        ])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3.append([ 87.2,  87.2,  86.8,  86. ,  86.4,  86.4,  86.4,  86.4,  86.4, 86.4,  86.4,  85.6,  84.8,  84.8,  84.8,  84.8,  84.8])
temp3 = np.array(temp3)
temp0.append(temp3)

# reformat the array
peakLowerBoundsListTemp = []
for i in xrange(40):
    temp = []
    temp.append(temp0[0][i])
    temp.append(temp0[1][i])
    peakLowerBoundsListTemp.append(temp)
    
peakLowerBoundsListList.append(peakLowerBoundsListTemp)



# UPPERBOUNDS
peakUpperBoundsListList = []

temp0 = []

# dx33, Al peak upperbound
temp3 = []
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])

temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])

temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])

temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3 = np.array(temp3)
temp0.append(temp3)

#dx33, Fe peak  upper bound
temp3 = []
temp3.append([ 156.05651665,  162.48824467,  162.1819719 ,  164.93842677,164.01960848,  163.71333572,  163.40706296,  164.01960848,163.40706296,  163.40706296,  161.26315362,  157.58788046])
temp3.append([ 158.37365591,  161.77419355,  162.39247312,  163.31989247,161.46505376,  161.15591398,  159.91935484,  160.22849462,160.53763441,  159.30107527,  157.13709677,  154.04569892])
temp3.append([ 158.37365591,  161.77419355,  162.39247312,  163.31989247,161.46505376,  161.15591398,  159.91935484,  160.22849462,160.53763441,  159.30107527,  157.13709677,  154.04569892])
temp3.append([ 146.20967742,  148.37365591,  150.22849462,  150.84677419,151.46505376,  150.53763441,  152.39247312,  150.53763441,149.91935484,  148.06451613,  145.28225806,  143.73655914])
temp3.append([ 149.31851587,  151.46242521,  153.30006178,  152.68751626,153.30006178,  152.68751626,  152.3812435 ,  151.76869797,150.84987968,  149.93106139,  147.17460653,  145.94951548])
temp3.append([ 147.86290323,  147.55376344,  148.79032258,  148.17204301,148.79032258,  148.4811828 ,  148.79032258,  147.55376344,147.55376344,  146.00806452,  145.08064516,  144.46236559])
temp3.append([ 179.65053763,  179.65053763,  177.48655914,  175.01344086,172.54032258,  170.0672043 ,  165.73924731,  164.19354839,162.33870968,  159.55645161,  153.37365591,  149.66397849])
temp3.append([ 185.16129032,  184.23387097,  183.30645161,  181.14247312,179.59677419,  179.59677419,  178.05107527,  177.4327957 ,176.50537634,  174.34139785,  172.48655914,  170.01344086])
temp3.append([ 147.62372675,  151.31050344,  152.53942901,  151.92496622,151.00327205,  151.00327205,  150.08157788,  150.08157788,148.85265231,  147.31649536,  143.93695005,  141.17186753])
temp3.append([ 137.66129032,  141.98924731,  143.84408602,  146.62634409,148.17204301,  149.71774194,  149.40860215,  150.02688172,148.79032258,  148.79032258,  146.00806452,  143.84408602])
temp3.append([ 129.26075269,  135.1344086 ,  135.75268817,  136.06182796,137.60752688,  138.84408602,  139.15322581,  138.53494624,138.84408602,  138.84408602,  136.98924731,  136.68010753])
temp3.append([ 141.88172043,  144.35483871,  145.90053763,  146.82795699,146.5188172 ,  147.13709677,  147.13709677,  146.82795699,148.06451613,  145.59139785,  144.04569892,  143.11827957])
temp3.append([ 152.23219761,  152.53942901,  149.77434648,  151.31050344,151.31050344,  150.69604066,  150.08157788,  149.46711509,148.23818953,  148.23818953,  144.24418144,  141.78633031])
temp3.append([ 145.69892473,  145.69892473,  145.38978495,  145.08064516,146.00806452,  145.38978495,  145.08064516,  144.46236559,143.22580645,  141.06182796,  141.37096774,  140.44354839])
temp3.append([ 162.33870968,  162.02956989,  161.72043011,  161.41129032,160.48387097,  160.48387097,  161.10215054,  161.72043011,159.8655914 ,  157.7016129 ,  156.15591398,  153.99193548])
temp3.append([ 159.19354839,  160.73924731,  159.81182796,  158.26612903,160.12096774,  158.26612903,  157.95698925,  157.33870968,157.02956989,  156.41129032,  154.8655914 ,  153.31989247])
temp3.append([ 142.36822597,  144.90676141,  146.03499938,  145.75293989,146.31705888,  146.88117787,  145.4708804 ,  144.06058293,144.34264242,  142.93234495,  140.67586901,  137.57321458])
temp3.append([ 145.69892473,  146.62634409,  147.55376344,  148.4811828 ,148.17204301,  149.09946237,  146.93548387,  148.17204301,147.86290323,  146.93548387,  145.38978495,  143.22580645])
temp3.append([ 138.22580645,  140.69892473,  141.3172043 ,  143.79032258,143.4811828 ,  145.64516129,  145.64516129,  145.33602151,146.88172043,  145.95430108,  143.79032258,  143.79032258])
temp3.append([ 143.73655914,  146.82795699,  148.99193548,  149.61021505,150.84677419,  152.08333333,  151.15591398,  153.31989247,153.01075269,  153.31989247,  147.13709677,  148.99193548])
temp3.append([ 155.06090318,  157.03531964,  157.03531964,  155.06090318,154.77884369,  155.90708166,  155.62502217,  155.34296267,155.34296267,  153.08648673,  150.54795128,  148.29147533])
temp3.append([ 147.55376344,  148.4811828 ,  148.79032258,  147.55376344,146.62634409,  147.24462366,  147.86290323,  147.86290323,147.24462366,  145.38978495,  146.00806452,  144.46236559])
temp3.append([ 148.11827957,  148.11827957,  148.73655914,  149.04569892,146.57258065,  147.5       ,  146.26344086,  146.88172043,146.88172043,  145.95430108,  142.24462366,  144.71774194])
temp3.append([ 144.35483871,  142.80913978,  144.66397849,  144.97311828,144.97311828,  145.59139785,  147.75537634,  146.82795699,144.97311828,  146.82795699,  145.59139785,  143.11827957])
temp3.append([ 138.66136662,  141.73283002,  141.42568368,  142.96141538,144.19000074,  145.72573244,  146.03287878,  147.26146414,145.72573244,  145.4185861 ,  144.49714708,  143.8828544 ])
temp3.append([ 152.15311005,  150.71770335,  149.28229665,  150.71770335,148.80382775,  148.80382775,  148.32535885,  147.84688995,147.36842105,  145.93301435,  142.58373206,  140.66985646])
temp3.append([ 158. ,  156.4,  156.8,  158. ,  156.8,  156.8,  155.2,  155.6, 154.8,  152.4,  151.2,  148.8,  153.6])
temp3.append([ 181.22852555,  181.22852555,  181.60631002,  181.60631002,180.47295659,  181.60631002,  175.93954284,  174.05062044,172.53948253,  171.40612909,  169.13942221,  168.38385325])
temp3.append([ 156.168708  ,  155.86156166,  155.86156166,  155.86156166,154.01868362,  154.32582996,  152.79009826,  152.79009826,152.48295192,  151.5615129 ,  149.71863486,  149.41148852,146.03287878,  146.03287878])
temp3.append([ 159.33014354,  159.33014354,  158.85167464,  156.93779904,157.41626794,  155.98086124,  155.50239234,  155.02392344,153.58851675,  153.11004785,  152.63157895,  151.67464115,148.32535885,  143.54066986])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3 = np.array(temp3)
temp0.append(temp3)

# reformat the array
peakUpperBoundsListTemp = []
for i in xrange(40):
    temp = []
    temp.append(temp0[0][i])
    temp.append(temp0[1][i])
    peakUpperBoundsListTemp.append(temp)

peakUpperBoundsListList.append(peakUpperBoundsListTemp)


temp0 = []

# dx34, Al peak upperbound
temp3 = []
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([75., 75., 75., 75., 75., 75., 75.])
temp3.append([75., 75., 75., 75., 75., 75., 75.])

temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([60., 60., 60., 60., 60., 60., 60.])
temp3.append([60., 60., 60., 60., 60., 60., 60.])
temp3.append([60., 60., 60., 60., 60., 60., 60.])
temp3.append([50., 50., 50., 50., 50., 50., 50.])
temp3.append([60., 60., 60., 60., 60., 60., 60.])
temp3.append([60., 60., 60., 60., 65., 65., 65.])

temp3.append([60., 60., 60., 60., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([70., 70., 70., 70., 70., 70., 70.])
temp3.append([50., 50., 50., 50., 50., 50., 50.])

temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])

temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])
temp3.append([65., 65., 65., 65., 65., 65., 65.])

temp3 = np.array(temp3)
temp0.append(temp3)

#dx34, Fe peak  upper bound, just copied from dx33
temp3 = []
temp3.append([ 156.05651665,  162.48824467,  162.1819719 ,  164.93842677,164.01960848,  163.71333572,  163.40706296,  164.01960848,163.40706296,  163.40706296,  161.26315362,  157.58788046])
temp3.append([ 158.37365591,  161.77419355,  162.39247312,  163.31989247,161.46505376,  161.15591398,  159.91935484,  160.22849462,160.53763441,  159.30107527,  157.13709677,  154.04569892])
temp3.append([ 158.37365591,  161.77419355,  162.39247312,  163.31989247,161.46505376,  161.15591398,  159.91935484,  160.22849462,160.53763441,  159.30107527,  157.13709677,  154.04569892])
temp3.append([ 146.20967742,  148.37365591,  150.22849462,  150.84677419,151.46505376,  150.53763441,  152.39247312,  150.53763441,149.91935484,  148.06451613,  145.28225806,  143.73655914])
temp3.append([ 149.31851587,  151.46242521,  153.30006178,  152.68751626,153.30006178,  152.68751626,  152.3812435 ,  151.76869797,150.84987968,  149.93106139,  147.17460653,  145.94951548])
temp3.append([ 147.86290323,  147.55376344,  148.79032258,  148.17204301,148.79032258,  148.4811828 ,  148.79032258,  147.55376344,147.55376344,  146.00806452,  145.08064516,  144.46236559])
temp3.append([ 179.65053763,  179.65053763,  177.48655914,  175.01344086,172.54032258,  170.0672043 ,  165.73924731,  164.19354839,162.33870968,  159.55645161,  153.37365591,  149.66397849])
temp3.append([ 185.16129032,  184.23387097,  183.30645161,  181.14247312,179.59677419,  179.59677419,  178.05107527,  177.4327957 ,176.50537634,  174.34139785,  172.48655914,  170.01344086])
temp3.append([ 147.62372675,  151.31050344,  152.53942901,  151.92496622,151.00327205,  151.00327205,  150.08157788,  150.08157788,148.85265231,  147.31649536,  143.93695005,  141.17186753])
temp3.append([ 137.66129032,  141.98924731,  143.84408602,  146.62634409,148.17204301,  149.71774194,  149.40860215,  150.02688172,148.79032258,  148.79032258,  146.00806452,  143.84408602])
temp3.append([ 129.26075269,  135.1344086 ,  135.75268817,  136.06182796,137.60752688,  138.84408602,  139.15322581,  138.53494624,138.84408602,  138.84408602,  136.98924731,  136.68010753])
temp3.append([ 141.88172043,  144.35483871,  145.90053763,  146.82795699,146.5188172 ,  147.13709677,  147.13709677,  146.82795699,148.06451613,  145.59139785,  144.04569892,  143.11827957])
temp3.append([ 152.23219761,  152.53942901,  149.77434648,  151.31050344,151.31050344,  150.69604066,  150.08157788,  149.46711509,148.23818953,  148.23818953,  144.24418144,  141.78633031])
temp3.append([ 145.69892473,  145.69892473,  145.38978495,  145.08064516,146.00806452,  145.38978495,  145.08064516,  144.46236559,143.22580645,  141.06182796,  141.37096774,  140.44354839])
temp3.append([ 162.33870968,  162.02956989,  161.72043011,  161.41129032,160.48387097,  160.48387097,  161.10215054,  161.72043011,159.8655914 ,  157.7016129 ,  156.15591398,  153.99193548])
temp3.append([ 159.19354839,  160.73924731,  159.81182796,  158.26612903,160.12096774,  158.26612903,  157.95698925,  157.33870968,157.02956989,  156.41129032,  154.8655914 ,  153.31989247])
temp3.append([ 142.36822597,  144.90676141,  146.03499938,  145.75293989,146.31705888,  146.88117787,  145.4708804 ,  144.06058293,144.34264242,  142.93234495,  140.67586901,  137.57321458])
temp3.append([ 145.69892473,  146.62634409,  147.55376344,  148.4811828 ,148.17204301,  149.09946237,  146.93548387,  148.17204301,147.86290323,  146.93548387,  145.38978495,  143.22580645])
temp3.append([ 138.22580645,  140.69892473,  141.3172043 ,  143.79032258,143.4811828 ,  145.64516129,  145.64516129,  145.33602151,146.88172043,  145.95430108,  143.79032258,  143.79032258])
temp3.append([ 143.73655914,  146.82795699,  148.99193548,  149.61021505,150.84677419,  152.08333333,  151.15591398,  153.31989247,153.01075269,  153.31989247,  147.13709677,  148.99193548])
temp3.append([ 155.06090318,  157.03531964,  157.03531964,  155.06090318,154.77884369,  155.90708166,  155.62502217,  155.34296267,155.34296267,  153.08648673,  150.54795128,  148.29147533])
temp3.append([ 147.55376344,  148.4811828 ,  148.79032258,  147.55376344,146.62634409,  147.24462366,  147.86290323,  147.86290323,147.24462366,  145.38978495,  146.00806452,  144.46236559])
temp3.append([ 148.11827957,  148.11827957,  148.73655914,  149.04569892,146.57258065,  147.5       ,  146.26344086,  146.88172043,146.88172043,  145.95430108,  142.24462366,  144.71774194])
temp3.append([ 144.35483871,  142.80913978,  144.66397849,  144.97311828,144.97311828,  145.59139785,  147.75537634,  146.82795699,144.97311828,  146.82795699,  145.59139785,  143.11827957])
temp3.append([ 138.66136662,  141.73283002,  141.42568368,  142.96141538,144.19000074,  145.72573244,  146.03287878,  147.26146414,145.72573244,  145.4185861 ,  144.49714708,  143.8828544 ])
temp3.append([ 152.15311005,  150.71770335,  149.28229665,  150.71770335,148.80382775,  148.80382775,  148.32535885,  147.84688995,147.36842105,  145.93301435,  142.58373206,  140.66985646])
temp3.append([ 158. ,  156.4,  156.8,  158. ,  156.8,  156.8,  155.2,  155.6, 154.8,  152.4,  151.2,  148.8,  153.6])
temp3.append([ 181.22852555,  181.22852555,  181.60631002,  181.60631002,180.47295659,  181.60631002,  175.93954284,  174.05062044,172.53948253,  171.40612909,  169.13942221,  168.38385325])
temp3.append([ 156.168708  ,  155.86156166,  155.86156166,  155.86156166,154.01868362,  154.32582996,  152.79009826,  152.79009826,152.48295192,  151.5615129 ,  149.71863486,  149.41148852,146.03287878,  146.03287878])
temp3.append([ 159.33014354,  159.33014354,  158.85167464,  156.93779904,157.41626794,  155.98086124,  155.50239234,  155.02392344,153.58851675,  153.11004785,  152.63157895,  151.67464115,148.32535885,  143.54066986])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])
temp3.append([ 152.13912065,  152.13912065,  151.38355169,  150.25019826,150.62798273,  149.11684482,  146.09456898,  143.82786211,142.69450867,  141.56115524,  140.4278018 ,  138.5388794 ,138.5388794 ])

temp3 = np.array(temp3)
temp0.append(temp3)

# reformat the array
peakUpperBoundsListTemp = []
for i in xrange(40):
    temp = []
    temp.append(temp0[0][i])
    temp.append(temp0[1][i])
    peakUpperBoundsListTemp.append(temp)

peakUpperBoundsListList.append(peakUpperBoundsListTemp)



# Region for fitting exponential background

backgroundRegion = [60, 80]

# make temporary containers
fitParametersListListList = []
gaussExpParametersListListList = []
backgroundListListList = []
backgroundpfitListListList = []   

#for j in xrange(len(filePrefixList)):   # cycle through datasets

for j in xrange(2):   # cycle through datasets
    
    # make temporary containers
    fitParametersListList = []
    gaussExpParametersListList = []
    backgroundListList = []
    backgroundpfitListList = []    
                        
    #for c in xrange(len(timeBoundariesListList[j][k])):  # cycle through list of time arrays            
    for c in xrange(1):  # cycle through list of time arrays            
        
        # make temporary containers
        fitParametersList = []
        gaussExpParametersList =[]
        backgroundList = []
        backgroundpfitList = []
 
        for k in xrange(len(dat[j])):  # cycle through dataset channels
        #for k in xrange(1):  # cycle through dataset channels
            
            # get peak fit boundaries
            peakUpperBoundsList = peakUpperBoundsListList[j][k][c]  # length = number of peaks to fit to
            peakLowerBoundsList = peakLowerBoundsListList[j][k][c]  # length = number of peaks to fit to
            
            peakLocationList = peakLocationListList[j][k][c]  # length = number of peaks to fit to
            timeBoundariesList = timeBoundariesListList[j][k][c]  # time boundaries

            # make temporary containers
            fitParameters = []
            gaussExpParameters = []
            background = []
            backgroundpfit = []

            for i in xrange(len(timeBoundariesList) - 1):  # cycle through time bins
                y = dat[j][k][:,timeBoundariesList[i]:timeBoundariesList[i+1]].sum(1)
                y = y / (timeBoundariesList[i+1] - timeBoundariesList[i])
                
                # Fit exponential to background section
                x = np.arange(256)
                #cut = (x > backgroundRegion[0]) & (x < backgroundRegion[1]) | ((x >= 17) & (x <= 19))
                cut = (x > backgroundRegion[0]) & (x < backgroundRegion[1])
                
                pfit = np.polyfit(x[cut], log(y[cut]), 1)
                backgroundpfit.append(pfit)
                
                # create background subtracted array
                y_bksub = y - exp(np.polyval(pfit, x))
                
                # Set starting parameters
                cut = (x > peakLowerBoundsList[i]) & (x < peakUpperBoundsList[i])
                startingParam = [max(y_bksub[cut]), peakLocationList[i], (peakUpperBoundsList[i] - peakLowerBoundsList[i])/10]
                try:
                    popt, pcov = curve_fit(gauss_function, x[cut], y_bksub[cut], p0 = startingParam)
                except:
                    print "Gaussian, Bad Fit"
                    popt = [-1, -1, -1]
                
                print popt
                fitMean = popt[1]
                fitSigma = popt[2]
                fitAmp = popt[0]
                
                ## fit a second time
                #upperBound = fitMean + 2.0 * fitSigma
                #lowerBound = fitMean - 1.0 * fitSigma
                
                #cut = (x > lowerBound) & (x < upperBound)
                #startingParam = [fitAmp, fitMean, fitSigma]
                #popt, pcov = curve_fit(gauss_function, x[cut], y[cut], p0 = startingParam)
                #print popt
                
                fitParameters.append(popt)
                fitMean = popt[1]
                fitSigma = popt[2]
                fitAmp = popt[0]        
                background.append(exp(np.polyval(pfit, fitMean)))
                
                # II. FIT DOUBLE EXPONENTIAL 
                
                # Get starting parameters for exponential
                cut = (x > peakLowerBoundsList[i]) & (x < peakUpperBoundsList[i])
                startingParamGauss = [max(y[cut]) - exp(np.polyval(pfit, fitMean)), \
                    peakLocationList[i], (peakUpperBoundsList[i] - peakLowerBoundsList[i])/10]

                startingParamExp = [exp(pfit[1]), pfit[0]]
                
                startingParam = startingParamGauss + startingParamExp
                cut = (x > peakLowerBoundsList[i]) & (x < peakUpperBoundsList[i])
                
                print 'dataset %d, time array %d, channel %d, time window %d' %(j, c, k, i)
                
                try:
                    popt, pcov = curve_fit(gauss_exp_function, x[cut], y[cut], p0 = startingParam)
                except:
                    print "Gaussian, Exp, Bad Fit"
                    popt = [-1, -1, -1, -1, -1]
                    
                print startingParam
                print popt
                gaussExpParameters.append(popt)
                
    
            # convert to np array
            fitParameters = np.array(fitParameters)
            gaussExpParameters = np.array(gaussExpParameters)
            backgroundpfit = np.array(backgroundpfit)
            background = np.array(background)
            
            # append to storage container
            # inserting stuff containing multiple time bins
            fitParametersList.append(fitParameters)
            gaussExpParametersList.append(gaussExpParameters)
            
            backgroundpfitList.append(backgroundpfit)
            backgroundList.append(background)

        # make temporary containers
        # inserting stuff containing multiple detectors
        fitParametersListList.append(fitParametersList)
        gaussExpParametersListList.append(gaussExpParametersList)
        backgroundListList.append(backgroundpfitList)
        backgroundpfitListList.append(backgroundList)   

    # Put in outer most container
    fitParametersListListList.append(fitParametersListList)
    gaussExpParametersListListList.append(gaussExpParametersListList)
    backgroundListListList.append(backgroundListList)
    backgroundpfitListListList.append(backgroundpfitListList)   



# PLOTS

# Plot the waveforms at multiple times bins, separate plots
index = 1
detNum = 28

filterSpectra = 0
filterWidth = 1

timeBoundariesList = np.hstack((np.arange(2,20,1), np.arange(20,100,10)))
#timeBoundariesList = np.hstack((np.arange(2,20,2), np.arange(20,100,10)))

detNumList = np.arange(9)

for detNum in detNumList:
    plt.figure()
    plt.grid()
    for i in xrange(len(timeBoundariesList)-1):    
        if filterSpectra:
            y = ndimage.filters.gaussian_filter(dat[index][detNum][:,timeBoundariesList[i]:timeBoundariesList[i+1]].sum(1), filterWidth, order = 0)
        else:
            y = dat[index][detNum][:,timeBoundariesList[i]:timeBoundariesList[i+1]].sum(1)
        y = y / (timeBoundariesList[i+1] - timeBoundariesList[i])
        
        plot(y, label = 't bin (%d,%d)' %(timeBoundariesList[i],timeBoundariesList[i+1]))
    
    plt.xlim((0, 255))
    plt.yscale('log')
    plt.legend()
    plt.ylabel('Counts')
    plt.xlabel('Energy')
    plt.yscale('log')
    plt.title('Dataset: %s, Detector # %d' %(filePrefixList[index], detNum+1))

params = {'legend.fontsize': 8,
          'legend.linewidth': 2}
plt.rcParams.update(params)




# Plot the waveforms at multiple times bins, SUBPLOTS
index = 1
#index = 6
peakIndex = 0

plotFits = 0
filterSpectra = 0
filterWidth = 1
normalize = 0

#timeBoundariesList = np.hstack((np.arange(2,20,2), np.arange(20,100,10)))
#timeBoundariesList = timeBoundariesList[0::2]

timeBoundariesList = np.array((2, 3, 4, 5, 6, 7, 9, 11))
#timeBoundariesList = np.array(np.hstack((np.arange(2,20,2), np.arange(20,60,10))))


subplotx = 4
subploty = 2


# cycle through detectors
for detNum in np.arange(40):
    # Generate a new figure window if filled.
    if ((detNum % (subplotx * subploty)) == 0):
        f, ax = plt.subplots(subploty, subplotx, sharex='col', sharey='row')
    # Calculate subplot window index
    ii = (detNum % (subplotx*subploty)) /subplotx
    jj = (detNum % (subplotx*subploty)) % subplotx
    
    for i in xrange(len(timeBoundariesList)-1):    
        if filterSpectra:
            y = ndimage.filters.gaussian_filter(dat[index][detNum][:,timeBoundariesList[i]:timeBoundariesList[i+1]].sum(1), filterWidth, order = 0)
        else:
            y = dat[index][detNum][:,timeBoundariesList[i]:timeBoundariesList[i+1]].sum(1)
        y = y / (timeBoundariesList[i+1] - timeBoundariesList[i])
        
        if normalize:
            y = y / sum(y[0:255])

        ax[ii][jj].plot(y, label = 't bin (%d,%d)' %(timeBoundariesList[i],timeBoundariesList[i+1]))
        
        if plotFits:
            # indices: data set index, peak index, detector number, time slice
            fitParams = gaussExpParametersListListList[index][peakIndex][detNum][i,:]
            peakUpperBounds = peakUpperBoundsListList[index][detNum][peakIndex][i] # length = number of peaks to fit to
            peakLowerBounds = peakLowerBoundsListList[index][detNum][peakIndex][i]  # length = number of peaks to fit to
            
            xarray = np.arange(round(peakLowerBounds), round(peakUpperBounds))
            ax[ii][jj].plot(xarray, gauss_exp_function(xarray, fitParams[0], fitParams[1], \
                fitParams[2], fitParams[3], fitParams[4] ))
            
    ax[ii][jj].grid()
    if (ii == 0) & (jj == 0):
        ax[ii][jj].legend()
    #ax[ii][jj].set_xlim((100, 200))
    ax[ii][jj].set_xlim((0, 75))
    
    #ax[ii][ii].set_ylim((1e-1, 1e5))
    ax[ii][jj].set_yscale('log')
    ax[ii][jj].set_ylabel('Counts')
    ax[ii][jj].set_xlabel('Energy')
    ax[ii][jj].set_title('Dataset: %s, Det# %d' %(filePrefixList[index], detNum+1))




# RATE AS FUNCTION OF TIME

index = 0
peakIndex = 0
detNumList = np.arange(9)

detNumList = detectorList['PS']
#detNumList = detectorList['FC']

timeBoundariesList = np.array((2, 3, 4, 5, 6, 7, 9, 11))
#timeBoundariesList = np.array(np.hstack((np.arange(2,20,2), np.arange(20,60,10))))

timeCenters = (timeBoundariesList[0:-1] +timeBoundariesList[1:])/2.0

curves = np.zeros((len(detNumList), len(gaussExpParametersListListList[index][peakIndex][detNum][:,0])))

plt.figure()
plt.grid()
i = 0

rateLifeTime = np.zeros(len(detNumList))
for detNum in detNumList:
    fitAmp = gaussExpParametersListListList[index][peakIndex][detNum][:,0]
    fitMean = gaussExpParametersListListList[index][peakIndex][detNum][:,1]
    fitSigma = abs(gaussExpParametersListListList[index][peakIndex][detNum][:,2]) 
    
    counts = np.zeros_like(fitAmp)

    for n in xrange(len(fitAmp)):
        xx = np.linspace(fitMean[n]-2.0*fitSigma[n], fitMean[n]+2.0*fitSigma[n], 50)
        counts[n] = sum(gauss_function(xx, fitAmp[n], fitMean[n], fitSigma[n])) * (xx[1] - xx[0])

    curves[i,:] = counts / counts[0]
    pfit = np.polyfit(timeCenters*timeCal, log(counts), 1)
        
    rateLifeTime[i] = -1/pfit[0]

    plt.plot(timeCenters*timeCal, counts, label = '%d, t = %3.3e' %(detNum+1, rateLifeTime[i]), marker = markerTypes[int(i/7)], \
        color = plotColors[i%7], markersize = 15)
    i += 1

pfit = np.polyfit(timeCenters*timeCal, log(curves.mean(axis = 0)), 1)

plt.xlabel('Time (sec)')
plt.ylabel('Count')

plt.title('%s, t_(average line) = %3.3e' %(filePrefixList[index], -1/pfit[0]));

plt.yscale('log')

params = {'legend.fontsize': 10, 'legend.linewidth': 2}
plt.rcParams.update(params)

plt.legend()



# FIT MEAN AS FUNCTION OF TIME


index = 1
peakIndex = 0
detNumList = np.arange(9)

detNumList = detectorList['PS']
#detNumList = detectorList['FC']

timeBoundariesList = np.array((2, 3, 4, 5, 6, 7, 9, 11))
#timeBoundariesList = np.array(np.hstack((np.arange(2,20,2), np.arange(20,60,10))))


timeCenters = (timeBoundariesList[0:-1] +timeBoundariesList[1:])/2.0

plt.figure()
plt.grid()
i = 0

curves = np.zeros((len(detNumList), len(gaussExpParametersListListList[index][peakIndex][detNum][:,0])))
fitParams = np.zeros((len(detNumList),3))

for detNum in detNumList:
    fitAmp = gaussExpParametersListListList[index][peakIndex][detNum][:,0]
    fitMean = gaussExpParametersListListList[index][peakIndex][detNum][:,1]
    fitSigma = abs(gaussExpParametersListListList[index][peakIndex][detNum][:,2]) 
    
    curves[detNum, :] = fitMean
    #pfit = np.polyfit(timeCenters[0:-2]*timeCal, log(fitMean[0:-2]), 1)
    #fitTimes[i] = -1/pfit[0]
    
    startingParam = [-(fitMean[-1] - fitMean[0]), -1/0.0003, fitMean[-1]]
    try:
        popt, pcov = curve_fit(inverted_exp_function, timeCenters[0:-1]*timeCal, fitMean[0:-1], p0 = startingParam)
    except:
        print "Bad Fit"
        popt = [-1, -1, -1]
    fitParams[i,:] = popt
    
    plt.plot(timeCenters*timeCal, fitMean, label = '%d, t = %3.3e' %(detNum+1, -1/fitParams[detNum,1]), \
        marker = markerTypes[int(i/7)], color = plotColors[i%7], markersize = 15)
        
    x = np.linspace(0, 0.0014, 50)
    y = inverted_exp_function(x, fitParams[i,0], fitParams[i,1], fitParams[i,2])
    plt.plot(x, y, '--k', color = plotColors[i%7], markersize = 15)
        
            
    i += 1
#plt.plot(timeCenters*timeCal, curves.mean(axis = 0), '--k', linewidth = 2)
cut = np.zeros(25) == 0
cut[21] = False

# fit to the means
#pfit = np.polyfit(timeCenters[0:-2]*timeCal, curves[cut,0:-2].mean(axis = 0), 1)
startingParam = [- (curves[cut,0:-2].mean(axis = 0)[-1] - curves[cut,0:-2].mean(axis = 0)[0]), -1./0.0003, 37]
popt, pcov = curve_fit(inverted_exp_function, timeCenters[0:-2]*timeCal, curves[cut,0:-2].mean(axis = 0), p0 = startingParam)

plt.plot(timeCenters*timeCal, curves[cut,:].mean(axis = 0), '--k', linewidth = 3)


plt.xlabel('Time (sec)')
plt.ylabel('Fit Mean')
plt.title('%s, Recovery Time of Average: %3.3e sec' %(filePrefixList[index], -1./popt[1]))

#plt.yscale('log')
params = {'legend.fontsize': 12,
          'legend.linewidth': 2}
plt.rcParams.update(params)
plt.legend(loc = 4)




# RATE IN H PEAK VERSUS H POSITION


index = 1
peakIndex = 0
detNumList = np.arange(9)

detNumList = detectorList['PS']
#detNumList = detectorList['FC']

#detNumList = np.array([26, 28, 29, 30, 36, 38])-1

timeBoundariesList = np.array((2, 3, 4, 5, 6, 7, 9, 11))
timeCenters = (timeBoundariesList[0:-1] +timeBoundariesList[1:])/2.0

plt.figure()
plt.grid()
i = 0

rateLifeTime = np.zeros((len(detNumList), gaussExpParametersListListList[index][peakIndex][detNum].shape[0]) )
for detNum in detNumList:
    fitAmp = gaussExpParametersListListList[index][peakIndex][detNum][:,0]
    fitMean = gaussExpParametersListListList[index][peakIndex][detNum][:,1]
    fitSigma = abs(gaussExpParametersListListList[index][peakIndex][detNum][:,2]) 
    
    counts = np.zeros_like(fitAmp)

    for n in xrange(len(fitAmp)):
        xx = np.linspace(fitMean[n]-2.0*fitSigma[n], fitMean[n]+2.0*fitSigma[n], 50)
        counts[n] = sum(gauss_function(xx, fitAmp[n], fitMean[n], fitSigma[n])) * (xx[1] - xx[0])

    plt.plot(fitMean, counts, label = '%d' %(detNum+1), marker = markerTypes[int(i/7)], \
        color = plotColors[i%7], markersize = 15)
    i += 1
plt.xlabel('Fit Mean')
plt.ylabel('Count')

plt.yscale('log')
plt.title('%s' %(filePrefixList[index]));
plt.legend()


# PLOT SPECTRA - FIRST SET
timeBoundariesList = timeBoundariesList1
#timeBoundariesList = np.hstack((np.arange(2,20,2), np.arange(20,60,10)))

#setIndex = 

datasetIndices = [6, 6, 6, 6]
detChList = [3, 3, 28, 28]
timeBinList = [timeBoundariesList1, timeBoundariesList2, timeBoundariesList1, timeBoundariesList2]


index = 6
detNum = 28
detNum = 3

k = 0

filterSpectra = 0
filterWidth = 1
plt.figure()
plt.grid()


i = 0
timeBoundary = [0, 1]
if filterSpectra:
    y = ndimage.filters.gaussian_filter(dat[index][detNum][:,timeBoundary[i]:timeBoundary[i+1]].sum(1), filterWidth, order = 0)
else:
    y = dat[index][detNum][:,timeBoundary[i]:timeBoundary[i+1]].sum(1)
y = y / (timeBoundary[i+1] - timeBoundary[i])

plot(y, label = 't bin (%d,%d)' %(timeBoundary[i],timeBoundary[i+1]))

timeBoundary = [1, 2]
i = 0
if filterSpectra:
    y = ndimage.filters.gaussian_filter(dat[index][detNum][:,timeBoundary[i]:timeBoundary[i+1]].sum(1), filterWidth, order = 0)
else:
    y = dat[index][detNum][:,timeBoundary[i]:timeBoundary[i+1]].sum(1)
y = y / (timeBoundary[i+1] - timeBoundary[i])

plot(y, label = 't bin (%d,%d)' %(timeBoundary[i],timeBoundary[i+1]))


for i in xrange(len(timeBoundariesList)-1):    

    if filterSpectra:
        y = ndimage.filters.gaussian_filter(dat[index][detNum][:,timeBoundariesList[i]:timeBoundariesList[i+1]].sum(1), filterWidth, order = 0)
    else:
        y = dat[index][detNum][:,timeBoundariesList[i]:timeBoundariesList[i+1]].sum(1)
    y = y / (timeBoundariesList[i+1] - timeBoundariesList[i])
    
    plot(y, label = 't bin (%d,%d)' %(timeBoundariesList[i],timeBoundariesList[i+1]))

    # over lay best fit to peak
    fitParam = fitParametersList[k]
    lowerBound = fitParam[i][1] - fitParam[i][2] * 2.0
    upperBound = fitParam[i][1] + fitParam[i][2] * 2.0
    
    xx = np.linspace(lowerBound, upperBound, 50)
    yy = gauss_function(xx, fitParam[i][0], fitParam[i][1], fitParam[i][2])

    plot(xx, yy)
    # plot the background level
    xx = np.linspace(20, 80, 50)
    pfit = backgroundpfitList[k][i,:]
    yy = exp(np.polyval(pfit, xx))
    plot(xx, yy)
    
plt.yscale('log')
plt.legend()
plt.ylabel('Counts')
plt.xlabel('Energy')
plt.yscale('log')
plt.title('Dataset: %s, Detector # %d' %(filePrefixList[index], detNum+1))






# H Peak count versus time
k = 0
timeCenters = (timeBinList[k][0:-1] + timeBinList[k][1:])/2.0
counts = fitParametersList[k][:,0] - backgroundList[k]

# fit to exponential
pfit = np.polyfit(timeCenters*timeCal, log(counts), 1)
t = -1/pfit[0]

plt.figure();
plt.grid()
plt.plot(timeCenters*timeCal, counts, 'xk', markersize = 15, linewidth = 2.)
xx = np.linspace(0, 0.00018, 50)
yy = exp(np.polyval(pfit, xx))
plot(xx, yy)

plt.xlabel('Time (sec)')
plt.ylabel('Counts')
plt.title('H Line Decay Time = %f sec' %t)




# H Peak location versus time


k = 0
timeCenters = (timeBinList[k][0:-1] + timeBinList[k][1:])/2.0
fitMeans = fitParametersList[k][:,1]

plt.figure();
plt.grid()
plt.plot(timeCenters*timeCal, fitMeans, 'xk', markersize = 15)

plt.xlabel('Time (sec)')
plt.ylabel('Fit Mean')
plt.title('H, 2.224 MeV')



# PLOT SPECTRA - SECOND SET - Fe peak

#timeBoundariesList = np.arange(2,11,1)
timeBoundariesList = np.hstack((np.arange(2,20,2), np.arange(20,60,10)))
index = 6
detNum = 28
detNum = 3

k = 1

filterSpectra = 0
filterWidth = 1
figure()
plt.grid()


timeBoundary = [0, 1]
i = 0
if filterSpectra:
    y = ndimage.filters.gaussian_filter(dat[index][detNum][:,timeBoundary[i]:timeBoundary[i+1]].sum(1), filterWidth, order = 0)
else:
    y = dat[index][detNum][:,timeBoundary[i]:timeBoundary[i+1]].sum(1)
y = y / (timeBoundary[i+1] - timeBoundary[i])

plot(y, label = 't bin (%d,%d)' %(timeBoundary[i],timeBoundary[i+1]))


timeBoundary = [1, 2]
i = 0
if filterSpectra:
    y = ndimage.filters.gaussian_filter(dat[index][detNum][:,timeBoundary[i]:timeBoundary[i+1]].sum(1), filterWidth, order = 0)
else:
    y = dat[index][detNum][:,timeBoundary[i]:timeBoundary[i+1]].sum(1)
y = y / (timeBoundary[i+1] - timeBoundary[i])

plot(y, label = 't bin (%d,%d)' %(timeBoundary[i],timeBoundary[i+1]))


for i in xrange(len(timeBoundariesList)-1):
    if filterSpectra:
        y = ndimage.filters.gaussian_filter(dat[index][detNum][:,timeBoundariesList[i]:timeBoundariesList[i+1]].sum(1), filterWidth, order = 0)
    else:
        y = dat[index][detNum][:,timeBoundariesList[i]:timeBoundariesList[i+1]].sum(1)
    y = y / (timeBoundariesList[i+1] - timeBoundariesList[i])
    
    # over lay best fit
    fitParam = fitParametersList[k]
    lowerBound = fitParam[i][1] - fitParam[i][2] * 2.0
    upperBound = fitParam[i][1] + fitParam[i][2] * 2.0
    
    xx = np.linspace(lowerBound, upperBound, 50)
    yy = gauss_function(xx, fitParam[i][0], fitParam[i][1], fitParam[i][2])
    
    plot(y, label = 't bin (%d,%d)' %(timeBoundariesList[i],timeBoundariesList[i+1]), color = plotColors[i], linestyle = lineStyles[i/7])
    plot(xx, yy, color = plotColors[i], linewidth = 2.0, linestyle = lineStyles[i/7])
plt.yscale('log')
plt.legend()
plt.ylabel('Counts')
plt.xlabel('Bin')
plt.yscale('log')
plt.title('Dataset: %s, Detector # %d' %(filePrefixList[index], detNum+1))



# Fe peak versus time
timeCal = 16.384e-6

k = 1
timeCenters = (timeBinList[k][0:-1] + timeBinList[k][1:])/2.0
fitMeans = fitParametersList[k][:,1]

plt.figure();
plt.grid()
plt.plot(timeCenters*timeCal, fitMeans, 'xk', markersize = 15)


plt.xlabel('Time (sec)')
plt.ylabel('Fit Mean')
plt.title('Fe, 7.645 MeV')

